<template>
  <div>

    <div class="top-panel__box">
        <el-form :model="dataForm" ref="dataForm" inline size="small" onsubmit="return false">
          <el-form-item>
            <el-button type="primary" @click="addRow()">
              <fa-icon class="fa-upload"/>
              新增
            </el-button>
          </el-form-item>
          <el-form-item>
            <el-input placeholder="机构名" v-model="dataForm.corporationName" maxlength="30" clearable />
          </el-form-item>
          <el-form-item>
            <el-select placeholder="全部状态" v-model="dataForm.corporationStatus" clearable>
                <el-option v-for="(value, key) in dicts['corporation_status'].childMap" :key="key" :label="value" :value="key"/>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="getList(1)">
              <fa-icon class="fa-search" />查询</el-button>
            <reset-button :dataForm.sync="dataForm" :fixedData="{pageNum: 1, pageSize: 20}" />
          </el-form-item>

        </el-form>

    </div>
    <el-row class="table-top">
      <el-table :data="dataList" v-loading="tableLoading" tooltip-effect="light" class="tc-table" size="small">
        <el-table-column type="index" width="55" fixed="left" label="序号"></el-table-column>
        <el-table-column prop="operation" class-name="oper-col" label="LOGO" :width="65">
          <template slot-scope="scope">
            <img :src="scope.row.imgUrl" alt="" width="40px" height="40px"/>
          </template>
        </el-table-column>
        <template v-for="(column, index) in BIZ_HEAD_LIST">
          <el-table-column :prop="column.prop" :label="column.label" :key="index" :width="column.width"
                           :show-overflow-tooltip="true"/>
        </template>
        <el-table-column prop="operation" class-name="oper-col" label="操作" :width="370">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" plain @click="updateStatus(scope.row)">
              {{ scope.row.corporationStatus === 'OPEN' ? '停用' : '启用' }}
            </el-button>
            <el-button size="mini" type="primary" plain @click="editRow(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" plain @click="deleteRow(scope.row)">删除</el-button>
            <el-dropdown>
              <el-button size="mini" type="primary" plain>
                新增产品<i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="addProd('Loan',scope.row)">贷款</el-dropdown-item>
                <el-dropdown-item @click.native="addProd('Card',scope.row)">信用卡</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <el-row class="pagination-top">
        <pagination :pageNum.sync="dataForm.pageNum" :curSizes.sync="dataForm.pageSize" :total="total"
                    @getList="getList"/>
      </el-row>
    </el-row>
    <el-dialog :key="JSON.stringify(editData)" :title="isEdit ?'编辑机构':'新增机构' " :visible.sync="editDialog" width="30%" :close-on-click-modal="false">
      <AddOrg :key="JSON.stringify(editData)" :isEdit="isEdit" :editData="editData" @uppdataSuccess="contentSuccess" @close="closeForm"/>
    </el-dialog>

    <el-dialog :key="addProdDialog" :title="prodType!=='Card' ?'新增贷款产品' : '新增信用卡产品'" :visible.sync="addProdDialog" width="70%"
               :close-on-click-modal="false">
      <AddCardProd v-if="prodType==='Card'" :edit-data="{}" :org="curOrg" @close="closeProd()"/>
      <AddLoanProd v-if="prodType==='Loan'" :edit-data="{}" :org="curOrg" @close="closeProd()"/>
    </el-dialog>
  </div>
</template>

<script>
import {BIZ_HEAD_LIST} from './js/contant.js'
import AddOrg from './components/AddOrg'
import {CONSTANT_OBJ} from '@/utils/constant'
import {common} from '@/utils/common'
import api from './api'
import {mapState} from 'vuex'
import {getCookie} from '@/utils/util'
import AddCardProd from '@/views/prodMsg/components/AddCardProd.vue'
import AddLoanProd from '@/views/prodMsg/components/AddLoanProd.vue'

export default {
  components: {AddLoanProd, AddCardProd, AddOrg},
  data () {
    return {
      BIZ_HEAD_LIST,
      editDialog: false,
      addProdDialog: false,
      tableLoading: false,
      isEdit: false,
      prodType: '',
      curOrg: {},
      editData: {},
      dataForm: {
        tenantId: getCookie('tenantId'),
        corporationName: '',
        corporationStatus: '',
        pageNum: 0,
        pageSize: 10
      },
      dataList: [],
      total: 0
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    // 获取列表数据
    getList (val) {
      this.dataForm.pageNum = val || this.dataForm.pageNum
      this.tableLoading = true
      api.findAllList(this.dataForm).then(res => {
        const {responseCode, data} = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          data.list.forEach(ele => {
            ele.newCorporationStatus = this.dicts['corporation_status'].childMap[ele.corporationStatus]
            ele.newProductNames = ele.productNames
          })
          this.dataList = data.list || []
          this.total = parseInt(data.total) || 0
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    // 新增
    addRow () {
      this.editData = {}
      this.editDialog = true
      this.isEdit = false
    },
    // 编辑
    editRow (row) {
      this.editData = {...row}
      this.editDialog = true
      this.isEdit = true
    },
    contentSuccess (data) {
      this.getList(1)
      this.editDialog = false
    },

    // 删除
    deleteRow (row) {
      common.confirmFn('删除后将无法恢复', '确认删除？').then(res => {
        if (res) {
          api.delCorporation(row.corporationId).then(this.successFn)
        }
      })
    },
    updateStatus (row) {
      const {corporationStatus} = row
      if (corporationStatus === 'OPEN') {
        common.confirmFn('停用后该机构下所有产品将不可见', '确认停用？').then(res => {
          if (res) {
            api.editCorporationStatus({...row, corporationStatus: 'CLOSE'}).then(this.successFn)
          }
        })
        return
      }
      api.editCorporationStatus({...row, corporationStatus: 'OPEN'}).then(this.successFn)
    },
    successFn (res, dialog) {
      const {responseCode, responseMsg: message} = res
      if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
        this.$message({
          message,
          type: 'success'
        })
        if (dialog) {
          this[dialog] = false
        }
        this.getList(1)
      }
    },
    closeForm () {
      this.editDialog = false
    },
    closeProd () {
      this.addProdDialog = false
    },
    addProd (type, row) {
      this.curOrg = row
      this.prodType = type
      this.addProdDialog = true
    }

  },
  computed: {
    ...mapState({
      dicts: state => state.common.dicts
    }),
  }
}
</script>
